﻿#region Disclaimer/Info

/////////////////////////////////////////////////////////////////////////////////////////////////
//
//   File:		AutoRefreshAttribute.cs
//   Website:		http://dexterblogengine.com/
//   Authors:		http://dexterblogengine.com/About.ashx
//   Rev:		1
//   Created:		19/01/2011
//   Last edit:		19/01/2011
//   License:		GNU Library General Public License (LGPL)
// 
//   For updated news and information please visit http://dexterblogengine.com/
//   Dexter is hosted to Codeplex at http://dexterblogengine.codeplex.com
//   For any question contact info@dexterblogengine.com
//
///////////////////////////////////////////////////////////////////////////////////////////////////

#endregion

using System;
using System.Security.Permissions;
using System.Web;
using System.Web.Mvc;

namespace Dexter.Web.Mvc.Filters {
	[AttributeUsage ( AttributeTargets.Class | AttributeTargets.Method , Inherited = true , AllowMultiple = false )]
	[AspNetHostingPermission ( SecurityAction.Demand , Level = AspNetHostingPermissionLevel.Minimal )]
	public class AutoRefreshAttribute : ActionFilterAttribute {
		public const int DEFAULT_DURATION_IN_SECONDS = 300;

		/// <summary>
		/// 	Initializes a new instance of the <see cref = "AutoRefreshAttribute" /> class.
		/// </summary>
		public AutoRefreshAttribute ( ) {
			DurationInSeconds = DEFAULT_DURATION_IN_SECONDS;
		}

		/// <summary>
		/// 	Gets or sets the duration in seconds.
		/// </summary>
		/// <value>The duration in seconds.</value>
		public int DurationInSeconds { get; set; }

		/// <summary>
		/// 	Gets or sets the name of the route.
		/// </summary>
		/// <value>The name of the route.</value>
		public string RouteName { get; set; }

		/// <summary>
		/// 	Gets or sets the name of the controller.
		/// </summary>
		/// <value>The name of the controller.</value>
		public string ControllerName { get; set; }

		/// <summary>
		/// 	Gets or sets the name of the action.
		/// </summary>
		/// <value>The name of the action.</value>
		public string ActionName { get; set; }

		/// <summary>
		/// 	Called by the MVC framework after the action result executes.
		/// </summary>
		/// <param name = "filterContext">The filter context.</param>
		public override void OnResultExecuted ( ResultExecutedContext filterContext ) {
			string url = BuildUrl ( filterContext );
			string headerValue = string.Concat ( DurationInSeconds , ";Url=" , url );

			filterContext.HttpContext.Response.AppendHeader ( "Refresh" , headerValue );

			base.OnResultExecuted ( filterContext );
		}

		string BuildUrl ( ControllerContext filterContext ) {
			var urlHelper = new UrlHelper ( filterContext.RequestContext );

			string url = !string.IsNullOrEmpty ( RouteName )
			             	? urlHelper.RouteUrl ( RouteName )
			             	: ( !string.IsNullOrEmpty ( ControllerName ) && !string.IsNullOrEmpty ( ActionName )
			             	    	? urlHelper.Action ( ActionName , ControllerName )
			             	    	: ( !string.IsNullOrEmpty ( ActionName )
			             	    	    	? urlHelper.Action ( ActionName )
			             	    	    	: filterContext.HttpContext.Request.RawUrl ) );

			return url;
		}
	}
}